ˆ Note that we often make a trade-off between time and space. ˆ Time complexity ˆ Space complexity. ˆ Unlike time, we can reuse memory.

Size: px
Start display at page:

Download "ˆ Note that we often make a trade-off between time and space. ˆ Time complexity ˆ Space complexity. ˆ Unlike time, we can reuse memory."

Transcription

1 ˆ We use O-notation to describe the asymptotic 1 upper bound of complexity of the algorithm. ˆ So O-notation is widely used to classify algorithms by how they respond to changes in its input size. 2 ˆ Time complexity ˆ Space complexity ˆ Note that we often make a trade-off between time and space. ˆ Unlike time, we can reuse memory. 1 The asymptotic sense is that the input size n grows toward infinity. 2 Actually, there are Θ, θ, o, Ω, and ω which are used to classify algorithms. Zheng-Liang Lu 143 / 180

2 Faster Is Better? ˆ Consider a 10-year deposit account. ˆ You deposit 10, 000 TWD in the beginning of each year. ˆ Assume that the bank pays an annual interest rate r = 10% compounded annually. ˆ Determine the balance at the end of 10 years. ˆ The answer is 175, TWD. Zheng-Liang Lu 144 / 180

3 Zheng-Liang Lu 145 / 180

4 Solution 1 1 clear; clc; 2 3 r = 0.1; 4 years = 10; 5 6 s = 1; 7 for i = 1 : years s = s * (1 + r) + 1; 9 end 10 s = s * (1 + r) * 1e4 ˆ Time complexity: O(n) ˆ Space complexity: O(1) ˆ It is similar to Horner s method. Zheng-Liang Lu 146 / 180

5 Solution 2 1 clear; clc; 2 3 r = 0.1; 4 years = 10; 5 6 s = 0; 7 for i = 1 : years 8 s = s + (1 + r) ˆ i; 9 end 10 s = s * 1e4 ˆ Time complexity: O(n) ˆ Space complexity: O(1) Zheng-Liang Lu 147 / 180

6 Solution 3 1 clear; clc; 2 3 r = 0.1; 4 years = 10; 5 s = (1 + r) * ((1 + r) ˆ years - 1) / r * 1e4 ˆ Time complexity: O(1) (Why?) ˆ Space complexity: O(1) ˆ Pros: fastest and compact; Cons: not robust Zheng-Liang Lu 148 / 180

7 Real Case: Floating Interest Rate ˆ Instead of constant interest rates, assume that the sequence of floating interest rates is r = 0.1 : 0.01 : ˆ Determine the compound amount. ˆ The balance is 125, TWD. Zheng-Liang Lu 149 / 180

8 Solution 1 clear; clc; 2 3 r = 0.1 : : 0.01; 4 years = 10; 5 6 s = 1; 7 for i = 1 : years s = s * (1 + r(i)) + 1; 9 end 10 s = s * (1 + r(end)) * 1e4 ˆ Trade-off: speed and generality Zheng-Liang Lu 150 / 180

9 Why Numerical Methods? ˆ Very few problems, however, have simple and analytical solutions. ˆ Numerical methods provide computational solutions for many crucial problems, thought the correctness of the numerical methods remains a big issue. 3 ˆ Simulation techniques 4 significantly reduce the cost both in industry and science. ˆ So the simulation techniques generate huge business profit. 3 See Generation_and_propagation_of_errors. 4 See Zheng-Liang Lu 151 / 180

10 All science is dominated by the idea of approximation. Bertrand Russell ( ) Essentially, all models are wrong, but some are useful. George E. P. Box ( ) Zheng-Liang Lu 152 / 180

11 Vectorization (Revisited) 5 ˆ The built-in functions such as sqrt(x) and exp(x) automatically operate on array arguments to produce an array result with the same size as the array argument x. 1 >> t = linspace(0, pi, 5); 2 >> y = sin(t) 3 4 y = More about vectorization. Zheng-Liang Lu 153 / 180

12 Advantages by Vectorization ˆ Appearance: vectorized mathematical code appears more like the mathematical expressions found in textbooks, making the code easier to understand. ˆ Less error prone: without loops, vectorized code is often shorter. ˆ Fewer lines of code mean fewer opportunities to introduce programming errors. ˆ Performance: vectorized code often runs much faster than the corresponding code containing loops. Zheng-Liang Lu 154 / 180

13 Performance Analysis In Real Time ˆ In addition to the theoretical analysis of algorithms, programmers can also use a timer to measure the performance. 6 ˆ Once you identify which functions are consuming the most time, you should determine why you are calling them, then look for alternatives to improve the overall performance. ˆ However, Amdahl s law 7 states that the speedup of a program using multiple processors in parallel computing is limited by the time needed for the sequential fraction of the program. 6 Note that the results may differ depending on the difference of run-time environments, so make sure that you benchmark the algorithms on the same conditions. 7 Amdahl (1967). Zheng-Liang Lu 155 / 180

14 Digress: Amdahl s Law ˆ Assume that a program has some codes which takes α% of total running time and can be further parallelized. ˆ Then the speedup can be achieved by investing p computers. ˆ More explicitly, Speedup = ˆ So we can calculate its limit, lim p α p α p (100 α) = + (100 α) 100 α. ˆ For example, the upper bound of speedup is 2 if α = 50. Zheng-Liang Lu 156 / 180

15 tic & toc ˆ The command tic makes a stopwatch timer start. ˆ The command toc returns the elapsed time ( 10 6 second) from the stopwatch timer started by tic. (Try.) 1 >> tic % Please wait for a second. 2 >> toc 3 4 Elapsed time is seconds. Zheng-Liang Lu 157 / 180

16 Tips for Performance 8 ˆ Preallocate arrays ˆ Repeatedly resizing arrays often requires Matlab to spend extra time looking for larger contiguous blocks of memory, and then moving the array into those blocks. ˆ Vectorize your code ˆ Create new variables if data type changes ˆ Use functions instead of scripts ˆ Avoid overloading Matlab built-in functions 8 See Techniques for Improving Performance. Zheng-Liang Lu 158 / 180

17 Example: A Benchmark 1 clear; clc; 2 3 order = 0 : 1 : 4; 4 t1 = zeros(1, length(order)); 5 t2 = zeros(1, length(order)); 6 t3 = zeros(1, length(order)); 7 8 for j = 1 : length(order) 9 num = 10 ˆ order(j); tic 12 y = []; 13 for i = 1 : num 14 y = [y, i ˆ 2]; % dynamic allocation of... array y 15 end 16 t1(j) = toc; Zheng-Liang Lu 159 / 180

18 17 18 clear y; 19 tic 20 y = zeros(1, num); % preallocation of array y 21 for i = 1 : num 22 y(i) = i ˆ 2; 23 end 24 t2(j) = toc; clear y; 27 t = 1 : 1 : num; 28 tic 29 y = t.ˆ 2; % vectorization 30 t3(j) = toc; 31 end Zheng-Liang Lu 160 / 180

19 t1 / t2 t1 / t3 t2 / t Speedup Array Size Zheng-Liang Lu 161 / 180

20 t1 / t2 t1 / t3 t2 / t3 Speedup (log scale) Array Size (log scale) Zheng-Liang Lu 162 / 180

21 All roads lead to Rome. Anonymous 但如你根本並無招式, 敵人如何來破你的招式? 風清揚 笑傲江湖 第十回 傳劍 Zheng-Liang Lu 163 / 180

22 1 >> Lecture 3 2 >> 3 >> -- Functions 4 >> Zheng-Liang Lu 164 / 180

23 Functions ˆ The first thing of algorithm design is to divide and conquer. ˆ In other words, a large and complicated problem would be conquered by solving its subproblems. ˆ In order to reuse the algorithms without copying the codes, the best way is to make them functions. 9 ˆ The idea of the (software) functions is similar to the math function, which is typically written in form of y = f (x) with the input x and the output y. 9 Make bricks for your castle. Zheng-Liang Lu 165 / 180

24 ˆ A function is a piece of computer code that accepts an input argument from the caller, and returns output argument for the specific job the caller dealing with. ˆ Functions allow you to modularize a program by separating its tasks into self-contained units. ˆ We can program more efficiently and avoid rewriting the computer code for calculations that are performed frequently Remember that the bug propagates when you copy and paste the codes. It is a serious problem especially when you are working on a medium- or huge-level project. Zheng-Liang Lu 166 / 180

25 Arithmetic Functions Zheng-Liang Lu 167 / 180

26 Trigonometric Functions 11 ˆ Recall that 1 rad = 180 π. 11 See Table in Palm, p Zheng-Liang Lu 168 / 180

27 Rounding Functions ˆ Can you organize an algorithm for the function ceil and floor? ˆ How to check if the input is an integer? (Try.) Zheng-Liang Lu 169 / 180

28 Discrete Math Functions Zheng-Liang Lu 170 / 180

29 Max Functions Zheng-Liang Lu 171 / 180

30 ˆ The function min works in a similar way to max. Zheng-Liang Lu 172 / 180

31 Functions for Central Tendency Zheng-Liang Lu 173 / 180

32 Variance and Standard Deviation Zheng-Liang Lu 174 / 180

33 Functions for Sizes Zheng-Liang Lu 175 / 180

34 Random Number Generators ˆ You may use randi(n, m, n) to produce an m-by-n random integer matrix ranging from 1 to N. Zheng-Liang Lu 176 / 180

35 ˆ You can generate a random number sampled from a standard uniform distribution, say by a linear congruential generator. 12 ˆ Be aware that there is no true random number generator in the machines. 13 ˆ Widely used in Monte Carlo simulation 14 and random number generation of other distributions 15. ˆ Use rng( shuffle ) to generate different random sequences. 12 See 13 For now, the modern computers are all deterministic. Quantum computers share theoretical similarities with non-deterministic and probabilistic computers. 14 See Glasserman (2003). 15 See the acceptance-rejection method and Metropolis-Hastings algorithm. Zheng-Liang Lu 177 / 180

36 User-Defined Functions ˆ A user-defined function is created by 1 function [outputvar] = funcname(inputvar) 2 % comment section 3 end ˆ The output variables, if there exist, are enclosed in square brackets. ˆ The input variables, if there exist, must be enclosed with parentheses. ˆ funcname should start with a letter, and be the same as the file name in which it is saved. ˆ Before this function can be used, it must be saved into the current folder If not, change the current folder or add to the path pool. Zheng-Liang Lu 178 / 180

37 Example: Addition of Two Numbers 1 function z = myadd(x, y) 2 % input: x, y (two numbers) 3 % output: z (sum of x and y) 4 z = x + y; 5 end ˆ It seems bloody trivial. ˆ Actually, the plus sign is a kind of syntactic sugar Recall how to use an addition instruction in assembly codes. Zheng-Liang Lu 179 / 180

38 Example: Mean of A Sequence 1 function y = mymean(x) 2 % input: x (array) 3 % output: y (mean) 4 5 sum = 0; 6 n = length(x); 7 for i = 1 : n 8 sum = myadd(sum, x(i)); % call myadd 9 end 10 y = sum / n; 11 end Zheng-Liang Lu 180 / 180

Exercise: Sorting 1. ˆ Let A be any array. ˆ Write a program which outputs the sorted array of A (in ascending order).

Exercise: Sorting 1. ˆ Let A be any array. ˆ Write a program which outputs the sorted array of A (in ascending order). Exercise: Sorting 1 ˆ Let A be any array. ˆ Write a program which outputs the sorted array of A (in ascending order). ˆ For example, A = [5, 4, 1, 2, 3]. ˆ Then the sorted array is [1, 2, 3, 4, 5]. ˆ You

More information

Jump Statements. ˆ The break statement exits a for or while loop completely.

Jump Statements. ˆ The break statement exits a for or while loop completely. Jump Statements ˆ The break statement exits a for or while loop completely. ˆ No longer in the loop. ˆ Aka early termination. ˆ To skip the rest of the instructions in the loop and begin the next iteration,

More information

1 >> Lecture 3 2 >> 3 >> -- Functions 4 >> Zheng-Liang Lu 172 / 225

1 >> Lecture 3 2 >> 3 >> -- Functions 4 >> Zheng-Liang Lu 172 / 225 1 >> Lecture 3 2 >> 3 >> -- Functions 4 >> Zheng-Liang Lu 172 / 225 Functions The first thing of the design of algorithms is to divide and conquer. A large and complex problem would be solved by couples

More information

Cloning Arrays. In practice, one might duplicate an array for some reason. One could attempt to use the assignment statement (=), for example,

Cloning Arrays. In practice, one might duplicate an array for some reason. One could attempt to use the assignment statement (=), for example, Cloning Arrays In practice, one might duplicate an array for some reason. One could attempt to use the assignment statement (=), for example, 1... 2 T[] A = {...}; // assume A is an array 3 T[] B = A;

More information

Cloning Arrays. In practice, one might duplicate an array for some reason. One could attempt to use the assignment statement (=), for example,

Cloning Arrays. In practice, one might duplicate an array for some reason. One could attempt to use the assignment statement (=), for example, Cloning Arrays In practice, one might duplicate an array for some reason. One could attempt to use the assignment statement (=), for example, 1... 2 T[] A = {...}; // assume A is an array 3 T[] B = A;

More information

How to swap values of two variables without tmp? However, this naive algorithm is biased. 1

How to swap values of two variables without tmp? However, this naive algorithm is biased. 1 Shuffling over array elements 1... 2 for (int i = 0; i < A.length; ++i) { 3 // choose j randomly 4 int j = (int) (Math.random() A.length); 5 // swap 6 int tmp = A[i]; 7 A[i] = A[j]; 8 A[j] = tmp; 9 } 10...

More information

1 class Lecture5 { 2 3 "Arrays" 4. Zheng-Liang Lu Java Programming 136 / 174

1 class Lecture5 { 2 3 Arrays 4. Zheng-Liang Lu Java Programming 136 / 174 1 class Lecture5 { 2 3 "Arrays" 4 5 } Zheng-Liang Lu Java Programming 136 / 174 Arrays An array stores a large collection of data which is of the same type. 2 // assume the size variable exists above 3

More information

1 >> Lecture 3 2 >> 3 >> -- Functions 4 >> Zheng-Liang Lu 169 / 221

1 >> Lecture 3 2 >> 3 >> -- Functions 4 >> Zheng-Liang Lu 169 / 221 1 >> Lecture 3 2 >> 3 >> -- Functions 4 >> Zheng-Liang Lu 169 / 221 Functions Recall that an algorithm is a feasible solution to the specific problem. 1 A function is a piece of computer code that accepts

More information

1 class Lecture6 { 2 3 "Methods" / References 8 [1] Ch. 5 in YDL 9 [1] Ch. 20 in YDL 10 / Zheng-Liang Lu Java Programming 185 / 248

1 class Lecture6 { 2 3 Methods / References 8 [1] Ch. 5 in YDL 9 [1] Ch. 20 in YDL 10 / Zheng-Liang Lu Java Programming 185 / 248 1 class Lecture6 { 2 3 "Methods" 4 5 } 6 7 / References 8 [1] Ch. 5 in YDL 9 [1] Ch. 20 in YDL 10 / Zheng-Liang Lu Java Programming 185 / 248 All roads lead to Rome. Anonymous 但如你根本並無招式, 敵人如何來破你的招式? 風清揚,

More information

1 class Lecture6 { 2 3 "Methods" // keywords: 8 return. Zheng-Liang Lu Java Programming 186 / 244

1 class Lecture6 { 2 3 Methods // keywords: 8 return. Zheng-Liang Lu Java Programming 186 / 244 1 class Lecture6 { 2 3 "Methods" 4 5 } 6 7 // keywords: 8 return Zheng-Liang Lu Java Programming 186 / 244 Methods 2 Methods can be used to define reusable code, and organize and simplify code. The idea

More information

Jump Statements. The keyword break and continue are often used in repetition structures to provide additional controls.

Jump Statements. The keyword break and continue are often used in repetition structures to provide additional controls. Jump Statements The keyword break and continue are often used in repetition structures to provide additional controls. break: the loop is terminated right after a break statement is executed. continue:

More information

Jump Statements. The keyword break and continue are often used in repetition structures to provide additional controls.

Jump Statements. The keyword break and continue are often used in repetition structures to provide additional controls. Jump Statements The keyword break and continue are often used in repetition structures to provide additional controls. break: the loop is terminated right after a break statement is executed. continue:

More information

Nested Loops. A loop can be nested inside another loop.

Nested Loops. A loop can be nested inside another loop. Nested Loops A loop can be nested inside another loop. Nested loops consist of an outer loop and one or more inner loops. Each time the outer loop is repeated, the inner loops are reentered, and started

More information

LECTURE 0: Introduction and Background

LECTURE 0: Introduction and Background 1 LECTURE 0: Introduction and Background September 10, 2012 1 Computational science The role of computational science has become increasingly significant during the last few decades. It has become the

More information

false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

false, import, new 1 class Lecture2 { 2 3 Data types, Variables, and Operators 4 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4 5 } 6 7 // Keywords: 8 byte, short, int, long, char, float, double, boolean, true, false, import, new Zheng-Liang Lu Java Programming 44

More information

false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

false, import, new 1 class Lecture2 { 2 3 Data types, Variables, and Operators 4 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4 5 } 6 7 // Keywords: 8 byte, short, int, long, char, float, double, boolean, true, false, import, new Zheng-Liang Lu Java Programming 45

More information

false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

false, import, new 1 class Lecture2 { 2 3 Data types, Variables, and Operators 4 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4 5 } 6 7 // Keywords: 8 byte, short, int, long, char, float, double, boolean, true, false, import, new Zheng-Liang Lu Java Programming 44

More information

Variables and Assignments

Variables and Assignments Variables and Assignments ˆ A variable is used to keep a value or values. ˆ A box which contains something. ˆ In most languages, a statement looks like var = expression, where var is a variable and expression

More information

Selections. Zheng-Liang Lu 91 / 120

Selections. Zheng-Liang Lu 91 / 120 Selections ˆ Selection enables us to write programs that make decisions on. ˆ Selection structures contain one or more of the if, else, and elseif statements. ˆ The end statement denotes the end of selection

More information

Introduction to Matlab Programming with Applications

Introduction to Matlab Programming with Applications Introduction to Matlab Programming with Applications Zheng-Liang Lu Department of Computer Science and Information Engineering National Taiwan University Matlab 256 Summer 2015 Class Information Official

More information

Introduction to Engineering gii

Introduction to Engineering gii 25.108 Introduction to Engineering gii Dr. Jay Weitzen Lecture Notes I: Introduction to Matlab from Gilat Book MATLAB - Lecture # 1 Starting with MATLAB / Chapter 1 Topics Covered: 1. Introduction. 2.

More information

Arrays. ˆ An array, is a linear data structure consisting of a collection of elements, each identified by one array index. ˆ For math, arrays could be

Arrays. ˆ An array, is a linear data structure consisting of a collection of elements, each identified by one array index. ˆ For math, arrays could be Arrays ˆ An array, is a linear data structure consisting of a collection of elements, each identified by one array index. ˆ For math, arrays could be ˆ row vectors: u R 1 n for any positive integer n ˆ

More information

Arithmetic Compound Assignment Operators

Arithmetic Compound Assignment Operators Arithmetic Compound Assignment Operators Note that these shorthand operators are not available in languages such as Matlab and R. Zheng-Liang Lu Java Programming 76 / 141 Example 1... 2 int x = 1; 3 System.out.println(x);

More information

switch-case Statements

switch-case Statements switch-case Statements A switch-case structure takes actions depending on the target variable. 2 switch (target) { 3 case v1: 4 // statements 5 break; 6 case v2: 7. 8. 9 case vk: 10 // statements 11 break;

More information

The return Statement

The return Statement The return Statement The return statement is the end point of the method. A callee is a method invoked by a caller. The callee returns to the caller if the callee completes all the statements (w/o a return

More information

Vectorization: An Introduction. Dr. Marco A. Arocha INGE3016-MATLAB Summer 2015

Vectorization: An Introduction. Dr. Marco A. Arocha INGE3016-MATLAB Summer 2015 Vectorization: An Introduction Dr. Marco A. Arocha INGE3016-MATLAB Summer 2015 1 Array and Matrix Operations Operation Operator Explanation Array addition a + b array addition and matrix addition are identical

More information

MBI REU Matlab Tutorial

MBI REU Matlab Tutorial MBI REU Matlab Tutorial Lecturer: Reginald L. McGee II, Ph.D. June 8, 2017 MATLAB MATrix LABoratory MATLAB is a tool for numerical computation and visualization which allows Real & Complex Arithmetics

More information

CSI33 Data Structures

CSI33 Data Structures Outline Department of Mathematics and Computer Science Bronx Community College September 6, 2017 Outline Outline 1 Chapter 2: Data Abstraction Outline Chapter 2: Data Abstraction 1 Chapter 2: Data Abstraction

More information

Data Types. 1 You cannot change the type of the variable after declaration. Zheng-Liang Lu Java Programming 52 / 87

Data Types. 1 You cannot change the type of the variable after declaration. Zheng-Liang Lu Java Programming 52 / 87 Data Types Java is a strongly-typed 1 programming language. Every variable has a type. Also, every (mathematical) expression has a type. There are two categories of data types: primitive data types, and

More information

false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

false, import, new 1 class Lecture2 { 2 3 Data types, Variables, and Operators 4 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4 5 } 6 7 // Keywords: 8 byte, short, int, long, char, float, double, boolean, true, false, import, new Zheng-Liang Lu Java Programming 44

More information

PART 1 PROGRAMMING WITH MATHLAB

PART 1 PROGRAMMING WITH MATHLAB PART 1 PROGRAMMING WITH MATHLAB Presenter: Dr. Zalilah Sharer 2018 School of Chemical and Energy Engineering Universiti Teknologi Malaysia 23 September 2018 Programming with MATHLAB MATLAB Environment

More information

Logic is the anatomy of thought. John Locke ( ) This sentence is false.

Logic is the anatomy of thought. John Locke ( ) This sentence is false. Logic is the anatomy of thought. John Locke (1632 1704) This sentence is false. I know that I know nothing. anonymous Plato (In Apology, Plato relates that Socrates accounts for his seeming wiser than

More information

Method Invocation. Zheng-Liang Lu Java Programming 189 / 226

Method Invocation. Zheng-Liang Lu Java Programming 189 / 226 Method Invocation Note that the input parameters are sort of variables declared within the method as placeholders. When calling the method, one needs to provide arguments, which must match the parameters

More information

Exercise (Revisited)

Exercise (Revisited) Exercise (Revisited) Redo the cashier problem by using an infinite loop with a break statement. 1... 2 while (true) { 3 System.out.println("Enter price?"); 4 price = input.nextint(); 5 if (price

More information

Introduction to Matlab Programming with Applications

Introduction to Matlab Programming with Applications Introduction to Matlab Programming with Applications Zheng-Liang Lu Department of Computer Science and Information Engineering National Taiwan University Matlab 289 Summer 2017 Class Information ˆ The

More information

Merge Sort. 25. Efficiency. Insertion sort vs. merge sort Timing with tic toc Time efficiency vs. memory efficiency

Merge Sort. 25. Efficiency. Insertion sort vs. merge sort Timing with tic toc Time efficiency vs. memory efficiency 25. Efficiency Insertion sort vs. merge sort Timing with tic toc Time efficiency vs. memory efficiency Announcements: - P6 will be posted today, due 11/29 - Final exam conflict? Email Kelly Patwell with

More information

Dr Richard Greenaway

Dr Richard Greenaway SCHOOL OF PHYSICS, ASTRONOMY & MATHEMATICS 4PAM1008 MATLAB 2 Basic MATLAB Operation Dr Richard Greenaway 2 Basic MATLAB Operation 2.1 Overview 2.1.1 The Command Line In this Workshop you will learn how

More information

Scope of Variables. In general, it is not a good practice to define many global variables. 1. Use global to declare x as a global variable.

Scope of Variables. In general, it is not a good practice to define many global variables. 1. Use global to declare x as a global variable. Scope of Variables The variables used in function m-files are known as local variables. Any variable defined within the function exists only for the function to use. The only way a function can communicate

More information

Floating Point Arithmetic

Floating Point Arithmetic Floating Point Arithmetic CS 365 Floating-Point What can be represented in N bits? Unsigned 0 to 2 N 2s Complement -2 N-1 to 2 N-1-1 But, what about? very large numbers? 9,349,398,989,787,762,244,859,087,678

More information

1 class Lecture5 { 2 3 "Methods" / References 8 [1] Ch. 5 in YDL 9 [1] Ch. 20 in YDL 0 / Zheng-Liang Lu Java Programming 176 / 199

1 class Lecture5 { 2 3 Methods / References 8 [1] Ch. 5 in YDL 9 [1] Ch. 20 in YDL 0 / Zheng-Liang Lu Java Programming 176 / 199 1 class Lecture5 { 2 3 "Methods" 4 5 } 6 7 / References 8 [1] Ch. 5 in YDL 9 [1] Ch. 20 in YDL 0 / Zheng-Liang Lu Java Programming 176 / 199 Methods 2 Methods can be used to define reusable code, and organize

More information

A Random Number Based Method for Monte Carlo Integration

A Random Number Based Method for Monte Carlo Integration A Random Number Based Method for Monte Carlo Integration J Wang and G Harrell Department Math and CS, Valdosta State University, Valdosta, Georgia, USA Abstract - A new method is proposed for Monte Carlo

More information

IEEE Floating-Point Representation 1

IEEE Floating-Point Representation 1 IEEE Floating-Point Representation 1 x = ( 1) s M 2 E The sign s determines whether the number is negative (s = 1) or positive (s = 0). The significand M is a fractional binary number that ranges either

More information

Stokes Modelling Workshop

Stokes Modelling Workshop Stokes Modelling Workshop 14/06/2016 Introduction to Matlab www.maths.nuigalway.ie/modellingworkshop16/files 14/06/2016 Stokes Modelling Workshop Introduction to Matlab 1 / 16 Matlab As part of this crash

More information

Chapter 1. Introduction

Chapter 1. Introduction Chapter 1 Introduction A Monte Carlo method is a compuational method that uses random numbers to compute (estimate) some quantity of interest. Very often the quantity we want to compute is the mean of

More information

Analytical Modeling of Parallel Programs

Analytical Modeling of Parallel Programs Analytical Modeling of Parallel Programs Alexandre David Introduction to Parallel Computing 1 Topic overview Sources of overhead in parallel programs. Performance metrics for parallel systems. Effect of

More information

Example: Monte Carlo Simulation 1

Example: Monte Carlo Simulation 1 Example: Monte Carlo Simulation 1 Write a program which conducts a Monte Carlo simulation to estimate π. 1 See https://en.wikipedia.org/wiki/monte_carlo_method. Zheng-Liang Lu Java Programming 133 / 149

More information

Only to be used for arranged hours. Order of Operations

Only to be used for arranged hours. Order of Operations Math 84 Activity # 1 Your name: Order of Operations Goals: 1) Evaluate Real numbers with Exponents. ) Use the Order of Operations to Evaluate Expressions. ) Review Exponents and Powers of Ten Integer exponents

More information

Variable Scope. The variable scope is the range of the program where the variable can be referenced.

Variable Scope. The variable scope is the range of the program where the variable can be referenced. Variable Scope The variable scope is the range of the program where the variable can be referenced. Variables can be declared in class level, method level, and loop level. In general, a pair of curly brackets

More information

Outline: Embarrassingly Parallel Problems. Example#1: Computation of the Mandelbrot Set. Embarrassingly Parallel Problems. The Mandelbrot Set

Outline: Embarrassingly Parallel Problems. Example#1: Computation of the Mandelbrot Set. Embarrassingly Parallel Problems. The Mandelbrot Set Outline: Embarrassingly Parallel Problems Example#1: Computation of the Mandelbrot Set what they are Mandelbrot Set computation cost considerations static parallelization dynamic parallelizations and its

More information

Introduction to MATLAB

Introduction to MATLAB to MATLAB Spring 2019 to MATLAB Spring 2019 1 / 39 The Basics What is MATLAB? MATLAB Short for Matrix Laboratory matrix data structures are at the heart of programming in MATLAB We will consider arrays

More information

IBM 370 Basic Data Types

IBM 370 Basic Data Types IBM 370 Basic Data Types This lecture discusses the basic data types used on the IBM 370, 1. Two s complement binary numbers 2. EBCDIC (Extended Binary Coded Decimal Interchange Code) 3. Zoned Decimal

More information

Common Errors double area; 3 if (r > 0); 4 area = r r 3.14; 5 System.out.println(area); 6... Zheng-Liang Lu Java Programming 101 / 141

Common Errors double area; 3 if (r > 0); 4 area = r r 3.14; 5 System.out.println(area); 6... Zheng-Liang Lu Java Programming 101 / 141 Common Errors 2 double area; 3 if (r > 0); 4 area = r r 3.14; 5 System.out.println(area); 6... Zheng-Liang Lu Java Programming 101 / 141 Generating random numbers Example Write a program which generates

More information

Introduction to Matlab

Introduction to Matlab Introduction to Matlab By:Mohammad Sadeghi *Dr. Sajid Gul Khawaja Slides has been used partially to prepare this presentation Outline: What is Matlab? Matlab Screen Basic functions Variables, matrix, indexing

More information

Worksheet 1. Hello Matlab

Worksheet 1. Hello Matlab Worksheet 1. Hello Matlab This worksheet gives a quick introduction to Matlab. Start Matlab from the START - PROGRAMS - MATLAB6.5 - MATLAB6.5 menu. You should now be faced by a terminal window awaiting

More information

COMP1730/COMP6730 Programming for Scientists. Data: Values, types and expressions.

COMP1730/COMP6730 Programming for Scientists. Data: Values, types and expressions. COMP1730/COMP6730 Programming for Scientists Data: Values, types and expressions. Lecture outline * Data and data types. * Expressions: computing values. * Variables: remembering values. What is data?

More information

CS240 Fall Mike Lam, Professor. Algorithm Analysis

CS240 Fall Mike Lam, Professor. Algorithm Analysis CS240 Fall 2014 Mike Lam, Professor Algorithm Analysis Algorithm Analysis Motivation: what and why Mathematical functions Comparative & asymptotic analysis Big-O notation ("Big-Oh" in textbook) Analyzing

More information

Global Variables. ˆ Unlike local variables, global variables are available to all functions involved.

Global Variables. ˆ Unlike local variables, global variables are available to all functions involved. Global Variables ˆ Unlike local variables, global variables are available to all functions involved. ˆ Use global to declare x as global. ˆ For example, the universal constant, say,. 1 ˆ However, it is

More information

Design of Parallel Algorithms. Course Introduction

Design of Parallel Algorithms. Course Introduction + Design of Parallel Algorithms Course Introduction + CSE 4163/6163 Parallel Algorithm Analysis & Design! Course Web Site: http://www.cse.msstate.edu/~luke/courses/fl17/cse4163! Instructor: Ed Luke! Office:

More information

Sankalchand Patel College of Engineering - Visnagar Department of Computer Engineering and Information Technology. Assignment

Sankalchand Patel College of Engineering - Visnagar Department of Computer Engineering and Information Technology. Assignment Class: V - CE Sankalchand Patel College of Engineering - Visnagar Department of Computer Engineering and Information Technology Sub: Design and Analysis of Algorithms Analysis of Algorithm: Assignment

More information

Arithmetic Compound Assignment Operators

Arithmetic Compound Assignment Operators Arithmetic Compound Assignment Operators Note that these shorthand operators are not available in languages such as Matlab and R. Zheng-Liang Lu Java Programming 76 / 172 Example 1... 2 int x = 1; 3 System.out.println(x);

More information

Ph3 Mathematica Homework: Week 1

Ph3 Mathematica Homework: Week 1 Ph3 Mathematica Homework: Week 1 Eric D. Black California Institute of Technology v1.1 1 Obtaining, installing, and starting Mathematica Exercise 1: If you don t already have Mathematica, download it and

More information

Introduction to Algorithms 6.046J/18.401J

Introduction to Algorithms 6.046J/18.401J Introduction to Algorithms 6.046J/18.401J LECTURE 1 Analysis of Algorithms Insertion sort Merge sort Prof. Charles E. Leiserson Course information 1. Staff. Prerequisites 3. Lectures 4. Recitations 5.

More information

Lecture Notes for Chapter 2: Getting Started

Lecture Notes for Chapter 2: Getting Started Instant download and all chapters Instructor's Manual Introduction To Algorithms 2nd Edition Thomas H. Cormen, Clara Lee, Erica Lin https://testbankdata.com/download/instructors-manual-introduction-algorithms-2ndedition-thomas-h-cormen-clara-lee-erica-lin/

More information

Plotting run-time graphically. Plotting run-time graphically. CS241 Algorithmics - week 1 review. Prefix Averages - Algorithm #1

Plotting run-time graphically. Plotting run-time graphically. CS241 Algorithmics - week 1 review. Prefix Averages - Algorithm #1 CS241 - week 1 review Special classes of algorithms: logarithmic: O(log n) linear: O(n) quadratic: O(n 2 ) polynomial: O(n k ), k 1 exponential: O(a n ), a > 1 Classifying algorithms is generally done

More information

ENGINEERING 1020 Introduction to Computer Programming M A Y 2 6, R E Z A S H A H I D I

ENGINEERING 1020 Introduction to Computer Programming M A Y 2 6, R E Z A S H A H I D I ENGINEERING 1020 Introduction to Computer Programming M A Y 2 6, 2 0 1 0 R E Z A S H A H I D I Today s class Constants Assignment statement Parameters and calling functions Expressions Mixed precision

More information

CSC630/CSC730 Parallel & Distributed Computing

CSC630/CSC730 Parallel & Distributed Computing CSC630/CSC730 Parallel & Distributed Computing Analytical Modeling of Parallel Programs Chapter 5 1 Contents Sources of Parallel Overhead Performance Metrics Granularity and Data Mapping Scalability 2

More information

C++ Programming Lecture 11 Functions Part I

C++ Programming Lecture 11 Functions Part I C++ Programming Lecture 11 Functions Part I By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department Introduction Till now we have learned the basic concepts of C++. All the programs

More information

Chapter 6: CPU Scheduling. Operating System Concepts 9 th Edition

Chapter 6: CPU Scheduling. Operating System Concepts 9 th Edition Chapter 6: CPU Scheduling Silberschatz, Galvin and Gagne 2013 Chapter 6: CPU Scheduling Basic Concepts Scheduling Criteria Scheduling Algorithms Thread Scheduling Multiple-Processor Scheduling Real-Time

More information

Case by Case. Chapter 3

Case by Case. Chapter 3 Chapter 3 Case by Case In the previous chapter, we used the conditional expression if... then... else to define functions whose results depend on their arguments. For some of them we had to nest the conditional

More information

1 Methods for Posterior Simulation

1 Methods for Posterior Simulation 1 Methods for Posterior Simulation Let p(θ y) be the posterior. simulation. Koop presents four methods for (posterior) 1. Monte Carlo integration: draw from p(θ y). 2. Gibbs sampler: sequentially drawing

More information

LAB: INTRODUCTION TO FUNCTIONS IN C++

LAB: INTRODUCTION TO FUNCTIONS IN C++ LAB: INTRODUCTION TO FUNCTIONS IN C++ MODULE 2 JEFFREY A. STONE and TRICIA K. CLARK COPYRIGHT 2014 VERSION 4.0 PALMS MODULE 2 LAB: FUNCTIONS IN C++ 2 Introduction This lab will provide students with an

More information

Variables are used to store data (numbers, letters, etc) in MATLAB. There are a few rules that must be followed when creating variables in MATLAB:

Variables are used to store data (numbers, letters, etc) in MATLAB. There are a few rules that must be followed when creating variables in MATLAB: Contents VARIABLES... 1 Storing Numerical Data... 2 Limits on Numerical Data... 6 Storing Character Strings... 8 Logical Variables... 9 MATLAB S BUILT-IN VARIABLES AND FUNCTIONS... 9 GETTING HELP IN MATLAB...

More information

Example. Write a program which generates 2 random integers and asks the user to answer the math expression.

Example. Write a program which generates 2 random integers and asks the user to answer the math expression. Generating random numbers Example Write a program which generates 2 random integers and asks the user to answer the math expression. For example, the program shows 2 + 5 =? If the user answers 7, then

More information

Graphics calculator instructions

Graphics calculator instructions Graphics calculator instructions Contents: A B C D E F G Basic calculations Basic functions Secondary function and alpha keys Memory Lists Statistical graphs Working with functions 10 GRAPHICS CALCULATOR

More information

COGS 119/219 MATLAB for Experimental Research. Fall Functions

COGS 119/219 MATLAB for Experimental Research. Fall Functions COGS 119/219 MATLAB for Experimental Research Fall 2016 - Functions User-defined Functions A user-defined function is a MATLAB program that is created by a user, saved as a function file, and then can

More information

Outline: Embarrassingly Parallel Problems

Outline: Embarrassingly Parallel Problems Outline: Embarrassingly Parallel Problems what they are Mandelbrot Set computation cost considerations static parallelization dynamic parallelizations and its analysis Monte Carlo Methods parallel random

More information

Computational Methods. Sources of Errors

Computational Methods. Sources of Errors Computational Methods Sources of Errors Manfred Huber 2011 1 Numerical Analysis / Scientific Computing Many problems in Science and Engineering can not be solved analytically on a computer Numeric solutions

More information

CSI33 Data Structures

CSI33 Data Structures Outline Department of Mathematics and Computer Science Bronx Community College August 31, 2015 Outline Outline 1 Chapter 1 Outline Textbook Data Structures and Algorithms Using Python and C++ David M.

More information

MEASURING COMPUTER TIME. A computer faster than another? Necessity of evaluation computer performance

MEASURING COMPUTER TIME. A computer faster than another? Necessity of evaluation computer performance Necessity of evaluation computer performance MEASURING COMPUTER PERFORMANCE For comparing different computer performances User: Interested in reducing the execution time (response time) of a task. Computer

More information

EL2310 Scientific Programming

EL2310 Scientific Programming (pronobis@kth.se) Overview Overview Wrap Up More on Scripts and Functions Basic Programming Lecture 2 Lecture 3 Lecture 4 Wrap Up Last time Loading data from file: load( filename ) Graphical input and

More information

1 class Lecture2 { 2 3 "Elementray Programming" / References 8 [1] Ch. 2 in YDL 9 [2] Ch. 2 and 3 in Sharan 10 [3] Ch.

1 class Lecture2 { 2 3 Elementray Programming / References 8 [1] Ch. 2 in YDL 9 [2] Ch. 2 and 3 in Sharan 10 [3] Ch. 1 class Lecture2 { 2 3 "Elementray Programming" 4 5 } 6 7 / References 8 [1] Ch. 2 in YDL 9 [2] Ch. 2 and 3 in Sharan 10 [3] Ch. 2 in HS 11 / Zheng-Liang Lu Java Programming 41 / 68 Example Given the radius

More information

Constraint-based Metabolic Reconstructions & Analysis H. Scott Hinton. Matlab Tutorial. Lesson: Matlab Tutorial

Constraint-based Metabolic Reconstructions & Analysis H. Scott Hinton. Matlab Tutorial. Lesson: Matlab Tutorial 1 Matlab Tutorial 2 Lecture Learning Objectives Each student should be able to: Describe the Matlab desktop Explain the basic use of Matlab variables Explain the basic use of Matlab scripts Explain the

More information

Introduction to Algorithms 6.046J/18.401J/SMA5503

Introduction to Algorithms 6.046J/18.401J/SMA5503 Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 1 Prof. Charles E. Leiserson Welcome to Introduction to Algorithms, Fall 01 Handouts 1. Course Information. Calendar 3. Registration (MIT students

More information

Chapter 2: Complexity Analysis

Chapter 2: Complexity Analysis Chapter 2: Complexity Analysis Objectives Looking ahead in this chapter, we ll consider: Computational and Asymptotic Complexity Big-O Notation Properties of the Big-O Notation Ω and Θ Notations Possible

More information

MAT 343 Laboratory 1 Matrix and Vector Computations in MATLAB

MAT 343 Laboratory 1 Matrix and Vector Computations in MATLAB MAT 343 Laboratory 1 Matrix and Vector Computations in MATLAB MATLAB is a computer software commonly used in both education and industry to solve a wide range of problems. This Laboratory provides a brief

More information

Python Lists: Example 1: >>> items=["apple", "orange",100,25.5] >>> items[0] 'apple' >>> 3*items[:2]

Python Lists: Example 1: >>> items=[apple, orange,100,25.5] >>> items[0] 'apple' >>> 3*items[:2] Python Lists: Lists are Python's compound data types. A list contains items separated by commas and enclosed within square brackets ([]). All the items belonging to a list can be of different data type.

More information

SECTION 1: INTRODUCTION. ENGR 112 Introduction to Engineering Computing

SECTION 1: INTRODUCTION. ENGR 112 Introduction to Engineering Computing SECTION 1: INTRODUCTION ENGR 112 Introduction to Engineering Computing 2 Course Overview What is Programming? 3 Programming The implementation of algorithms in a particular computer programming language

More information

Numerical Analysis First Term Dr. Selcuk CANKURT

Numerical Analysis First Term Dr. Selcuk CANKURT ISHIK UNIVERSITY FACULTY OF ENGINEERING and DEPARTMENT OF COMPUTER ENGINEERING Numerical Analysis 2017-2018 First Term Dr. Selcuk CANKURT selcuk.cankurt@ishik.edu.iq Textbook Main Textbook MATLAB for Engineers,

More information

Numerical Solutions of Differential Equations (1)

Numerical Solutions of Differential Equations (1) Numerical Solutions of Differential Equations (1) Euler method Sources of error Truncation Round-off error Error balance in numerical integration Euler Method Many differential equations don't have an

More information

1 class Lecture3 { 2 3 "Selections" // Keywords 8 if, else, else if, switch, case, default. Zheng-Liang Lu Java Programming 89 / 137

1 class Lecture3 { 2 3 Selections // Keywords 8 if, else, else if, switch, case, default. Zheng-Liang Lu Java Programming 89 / 137 1 class Lecture3 { 2 3 "Selections" 4 5 } 6 7 // Keywords 8 if, else, else if, switch, case, default Zheng-Liang Lu Java Programming 89 / 137 Flow Controls The basic algorithm (and program) is constituted

More information

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan Lecture 08-1 Programming in C++ PART 1 By Assistant Professor Dr. Ali Kattan 1 The Conditional Operator The conditional operator is similar to the if..else statement but has a shorter format. This is useful

More information

Getting Started with Matlab

Getting Started with Matlab Chapter Getting Started with Matlab The computational examples and exercises in this book have been computed using Matlab, which is an interactive system designed specifically for scientific computation

More information

Lecture 2: Analyzing Algorithms: The 2-d Maxima Problem

Lecture 2: Analyzing Algorithms: The 2-d Maxima Problem Lecture 2: Analyzing Algorithms: The 2-d Maxima Problem (Thursday, Jan 29, 1998) Read: Chapter 1 in CLR. Analyzing Algorithms: In order to design good algorithms, we must first agree the criteria for measuring

More information

1 class Lecture3 { 2 3 "Selections" // Keywords 8 if, else, else if, switch, case, default. Zheng-Liang Lu Java Programming 88 / 133

1 class Lecture3 { 2 3 Selections // Keywords 8 if, else, else if, switch, case, default. Zheng-Liang Lu Java Programming 88 / 133 1 class Lecture3 { 2 3 "Selections" 4 5 } 6 7 // Keywords 8 if, else, else if, switch, case, default Zheng-Liang Lu Java Programming 88 / 133 Flow Controls The basic algorithm (and program) is constituted

More information

Course Layout. Go to https://www.license.boun.edu.tr, follow instr. Accessible within campus (only for the first download)

Course Layout. Go to https://www.license.boun.edu.tr, follow instr. Accessible within campus (only for the first download) Course Layout Lectures 1: Variables, Scripts and Operations 2: Visualization and Programming 3: Solving Equations, Fitting 4: Images, Animations, Advanced Methods 5: Optional: Symbolic Math, Simulink Course

More information

Outline. CSE 1570 Interacting with MATLAB. Starting MATLAB. Outline (Cont d) MATLAB Windows. MATLAB Desktop Window. Instructor: Aijun An

Outline. CSE 1570 Interacting with MATLAB. Starting MATLAB. Outline (Cont d) MATLAB Windows. MATLAB Desktop Window. Instructor: Aijun An CSE 170 Interacting with MATLAB Instructor: Aijun An Department of Computer Science and Engineering York University aan@cse.yorku.ca Outline Starting MATLAB MATLAB Windows Using the Command Window Some

More information

HOW TO WRITE PARALLEL PROGRAMS AND UTILIZE CLUSTERS EFFICIENTLY

HOW TO WRITE PARALLEL PROGRAMS AND UTILIZE CLUSTERS EFFICIENTLY HOW TO WRITE PARALLEL PROGRAMS AND UTILIZE CLUSTERS EFFICIENTLY Sarvani Chadalapaka HPC Administrator University of California Merced, Office of Information Technology schadalapaka@ucmerced.edu it.ucmerced.edu

More information

Example. Generating random numbers. Write a program which generates 2 random integers and asks the user to answer the math expression.

Example. Generating random numbers. Write a program which generates 2 random integers and asks the user to answer the math expression. Example Generating random numbers Write a program which generates 2 random integers and asks the user to answer the math expression. For example, the program shows 2 + 5 =? If the user answers 7, then

More information

MATLAB Basics. Configure a MATLAB Package 6/7/2017. Stanley Liang, PhD York University. Get a MATLAB Student License on Matworks

MATLAB Basics. Configure a MATLAB Package 6/7/2017. Stanley Liang, PhD York University. Get a MATLAB Student License on Matworks MATLAB Basics Stanley Liang, PhD York University Configure a MATLAB Package Get a MATLAB Student License on Matworks Visit MathWorks at https://www.mathworks.com/ It is recommended signing up with a student

More information

Numerical Methods in Scientific Computation

Numerical Methods in Scientific Computation Numerical Methods in Scientific Computation Programming and Software Introduction to error analysis 1 Packages vs. Programming Packages MATLAB Excel Mathematica Maple Packages do the work for you Most

More information